package niit.wei.service.impl;

import niit.wei.dao.UserDao;
import niit.wei.dao.impl.UserDaoImpl;
import niit.wei.entity.User;
import niit.wei.service.UserService;
import niit.wei.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * @Author WeiJinLong
 * @Date 2023-06-29 11:45
 * @Version 1.0
 */
public class UserServiceImpl implements UserService {
    private UserDao userDao = new UserDaoImpl();

    @Override
    public User login(String email, String password) {
        User user = null;
        Connection conn = null;
        try {
            conn = JDBCUtils.getConnection();
            conn.setAutoCommit(false);
            String sql = "select user_id userId,email,password from User where email=? and password=?";
            user = userDao.getUser(conn, sql, User.class, email, password);
            conn.commit();
        } catch (SQLException | ClassNotFoundException | NoSuchFieldException | RuntimeException |
                 InstantiationException | IllegalAccessException e) {
            try {
                conn.rollback();
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
            throw new RuntimeException(e);
        }
        return user;
    }
}
